package com.sky.controller.admin;

import com.sky.annatation.AutoFill;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "套餐接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @PostMapping
    @Operation(summary = "新增套餐")
    @AutoFill(value = OperationType.INSERT)
    @CacheEvict(cacheNames = "setmealCache",key = "#dto.categoryId")
    public Result save(@RequestBody SetmealDTO dto){
        setmealService.save(dto);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO dto){
        PageResult pageResult = setmealService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @Operation(summary = "批量删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询")
    public Result<SetmealVO> getById(@PathVariable Long id){
        SetmealVO setmealvo = setmealService.getById(id);
        return Result.success(setmealvo);
    }

    /**
     * 更新套餐信息
     * @param dto
     * @return
     */
    @AutoFill(value = OperationType.UPDATE)
    @PutMapping
    @Operation(summary = "更新套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO dto){
        setmealService.update(dto);
        return Result.success();
    }

    /**
     * 套餐启用停用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result setmealStatus(@PathVariable Integer status,Long id){
        setmealService.setmealStatus(status,id);
        return Result.success();
    }
}
